<?php

class Model_Publisher
{

    public static function getAll ()
    {
        try {
            
            $db = Core_Global::getDatabase();
            
            $stmt = $db->query('SELECT * FROM Publisher WHERE Status = ?', 
                    array(
                            1
                    ));
            
            return $stmt->fetchAll();
        } catch (Zend_Db_Exception $e) {
            throw new Zend_Db_Exception($e);
        }
    }

    public static function get ($params)
    {
        try {
            
            $db = Core_Global::getDatabase();
            
            $stmt = $db->query(
                    'SELECT * FROM Publisher WHERE PublisherID = ? AND Status = ?', 
                    array(
                            $params['publisher_id'],
                            1
                    ));
            
            return $stmt->fetch();
        } catch (Zend_Db_Exception $e) {
            throw new Zend_Db_Exception($e);
        }
    }

    public static function find ($params)
    {
        try {
            
            $db = Core_Global::getDatabase();
            
            $stmt = $db->query('SELECT * FROM Publisher WHERE Name LIKE "%' . $params['q'] . '%" AND Status = ?', 
                    array(                        
                        1
                    ));
            
            return $stmt->fetchAll();
        } catch (Zend_Db_Exception $e) {
            throw new Zend_Db_Exception($e);
        }
    }

    public static function create ($params)
    {
        try {
            
            $db = Core_Global::getDatabase();
            
            $stmt = $db->query('INSERT INTO Publisher VALUES (?,?,?,?)', 
                    array(
                            null,
                            $params['Name'],
                            intval(microtime(true)),
                            1
                    ));
            
            return $stmt->rowCount();
        } catch (Zend_Db_Exception $e) {
            throw new Zend_Db_Exception($e);
        }
    }

    public static function edit ($params)
    {
        try {
            
            $db = Core_Global::getDatabase();
            
            $stmt = $db->query(
                    'UPDATE Publisher SET Name = ? WHERE PublisherID = ?', 
                    array(
                            $params['Name'],
                            $params['PublisherID']
                    ));
            
            return $stmt->rowCount();
        } catch (Zend_Db_Exception $e) {
            throw new Zend_Db_Exception($e);
        }
    }

    public static function delete ($params)
    {
        try {
            
            $db = Core_Global::getDatabase();
            
            $stmt = $db->query('DELETE FROM Publisher WHERE PublisherID = ?', 
                    array(
                            $params['PublisherID']
                    ));
            
            return $stmt->rowCount();
        } catch (Zend_Db_Exception $e) {
            throw new Zend_Db_Exception($e);
        }
    }
}
